package com.moody.hibernate.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.ClassifyRecord;
import com.moody.hibernate.domain.CustDefaultEvent;

/**
 * A data access object (DAO) providing persistence and search support for
 * CustDefaultEvent entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.CustDefaultEvent
 * @author MyEclipse Persistence Tools
 */

public class CustDefaultEventDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(CustDefaultEventDAO.class);
	// property constants
	public static final String CUST_ID = "custId";
	public static final String CREDIT_LEVEL_BF = "creditLevelBf";
	public static final String EVENT_TYPE = "eventType";
	public static final String EVENT_DESC = "eventDesc";
	public static final String EVENT_DATE = "eventDate";
	public static final String STATUS = "status";
	public static final String INPUT_USER_ID = "inputUserId";
	public static final String INPUT_ORG_ID = "inputOrgId";
	public static final String INPUT_DATE = "inputDate";
	public static final String CONFIRM_USER_ID = "confirmUserId";
	public static final String CONFIRM_ORG_ID = "confirmOrgId";
	public static final String CONFIRM_DATE = "confirmDate";

	protected void initDao() {
		// do nothing
	}

	public void save(CustDefaultEvent transientInstance) {
		log.debug("saving CustDefaultEvent instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CustDefaultEvent persistentInstance) {
		log.debug("deleting CustDefaultEvent instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CustDefaultEvent findById(java.lang.String id) {
		log.debug("getting CustDefaultEvent instance with id: " + id);
		try {
			CustDefaultEvent instance = (CustDefaultEvent) getHibernateTemplate()
					.get("com.moody.hibernate.domain.CustDefaultEvent", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(CustDefaultEvent instance) {
		log.debug("finding CustDefaultEvent instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CustDefaultEvent instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from CustDefaultEvent as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCustId(Object custId) {
		return findByProperty(CUST_ID, custId);
	}

	public List findByCreditLevelBf(Object creditLevelBf) {
		return findByProperty(CREDIT_LEVEL_BF, creditLevelBf);
	}

	public List findByEventType(Object eventType) {
		return findByProperty(EVENT_TYPE, eventType);
	}

	public List findByEventDesc(Object eventDesc) {
		return findByProperty(EVENT_DESC, eventDesc);
	}

	public List findByEventDate(Object eventDate) {
		return findByProperty(EVENT_DATE, eventDate);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findByInputUserId(Object inputUserId) {
		return findByProperty(INPUT_USER_ID, inputUserId);
	}

	public List findByInputOrgId(Object inputOrgId) {
		return findByProperty(INPUT_ORG_ID, inputOrgId);
	}

	public List findByInputDate(Object inputDate) {
		return findByProperty(INPUT_DATE, inputDate);
	}

	public List findByConfirmUserId(Object confirmUserId) {
		return findByProperty(CONFIRM_USER_ID, confirmUserId);
	}

	public List findByConfirmOrgId(Object confirmOrgId) {
		return findByProperty(CONFIRM_ORG_ID, confirmOrgId);
	}

	public List findByConfirmDate(Object confirmDate) {
		return findByProperty(CONFIRM_DATE, confirmDate);
	}

	public List findAll() {
		log.debug("finding all CustDefaultEvent instances");
		try {
			String queryString = "from CustDefaultEvent";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CustDefaultEvent merge(CustDefaultEvent detachedInstance) {
		log.debug("merging CustDefaultEvent instance");
		try {
			CustDefaultEvent result = (CustDefaultEvent) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CustDefaultEvent instance) {
		log.debug("attaching dirty CustDefaultEvent instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CustDefaultEvent instance) {
		log.debug("attaching clean CustDefaultEvent instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CustDefaultEventDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (CustDefaultEventDAO) ctx.getBean("CustDefaultEventDAO");
	}

	public void update(CustDefaultEvent transientInstance) {
		log.debug("updating CustDefaultEvent instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
		
	}

	public List<CustDefaultEvent> findDEByCustId(String custId) {
		List<CustDefaultEvent> list = new ArrayList<CustDefaultEvent>();
		 try{
			 String queryString = "from CustDefaultEvent as cde where cde.custId = ? order by status asc, confirmDate desc";
			 list =  getHibernateTemplate().find(queryString, custId);
			 }catch (Exception e){
				 log.error("query error", e);
			}
			 return list;
	}
}